<template>
  <div ref="echartDom"></div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose} from 'vue'
import {pxToRem} from "@/utils/index";

const props = defineProps({
  lineData: {
    type: Object,
    default: () => {
    }
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const echartDom = ref(null);
const myChart = shallowRef(null);
const echarts = inject('echarts');

const initEchart = () => {
  if (echartDom.value) {
    let tempSeriesData = [
      {
        data: props?.lineData?.seriesData[0],
        type: 'line',
        name: props?.lineData?.legendData[0]?.name,
        smooth: true,
        showSymbol: false,
        itemStyle: {
          color: props?.lineData?.legendData[0]?.itemStyle?.color
        },
        areaStyle: {
          opacity: 0.8,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: props?.lineData?.linearGradientColor[0]?.[0]
            },
            {
              offset: 1,
              color: props?.lineData?.linearGradientColor[0]?.[1]
            }
          ])
        },
      },
      {
        data: props?.lineData?.seriesData[1],
        type: 'line',
        name: props?.lineData?.legendData[1]?.name,
        smooth: true,
        showSymbol: false,
        itemStyle: {
          color: props?.lineData?.legendData[1]?.itemStyle?.color
        },
        areaStyle: {
          opacity: 0.8,
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            {
              offset: 0,
              color: props?.lineData?.linearGradientColor[1]?.[0]
            },
            {
              offset: 1,
              color: props?.lineData?.linearGradientColor[1]?.[1]
            }
          ])
        },
      }
    ]
    myChart.value = echarts.init(echartDom.value);
    let option = {
      grid: {
        left: '3%',
        right: '3%',
        bottom: 0,
        top: '20%',
        containLabel: true
      },
      legend: {
        x: 'right',
        icon: 'rect',
        itemWidth: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        itemHeight: pxToRem(10 * props?.dialogData?.fontSizeTimes),
        textStyle: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes)
        },
        data: props?.lineData?.legendData
      },
      xAxis: {
        min: 0,
        type: 'category',
        boundaryGap: false,
        data: props?.lineData?.xAxisData,
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter',
          showMinLabel: false,
          interval: 0,
          formatter: (value) => {
            if (props?.lineData?.isCustomXAxisLabel) {
              const tempLabelArray = value.split('-')
              return `${tempLabelArray[1]}月`
            } else {
              return `${value}月`
            }
          }
        },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: true, // 不显示坐标轴线
          lineStyle: {
            color: '#28FCFF'
          }
        },
      },
      yAxis: {
        name: `单位：${props?.lineData?.yAxisName}`,
        min: 0,
        max: (value) => value.max || props?.lineData?.yAxisDefaultMaxValue,
        nameTextStyle: {
          color: "white",
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          fontWeight: 'lighter'
        },
        textStyle: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes)
        },
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: false, // 不显示坐标轴线
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: 'rgba(49,103,156,0.6)',
            type: 'dashed'
          }
        },
        axisLabel: {
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          color: 'white',
          fontWeight: 'lighter'
        }
      },
      series: tempSeriesData
    }
    option && myChart.value.setOption(option, true);

    myChart.value.on('legendselectChanged', (params) => {
      event.stopPropagation();
    })
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};


defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h250 {
  height: 250px;
}
.dialog-h250 {
  width: 1120px;
  height: 500px;
}
</style>